// src/stores/searchHistory.js
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useSearchHistoryStore = defineStore(
  'searchHistory',
  () => {
    const searchHistory = ref([])

    const addSearch = (searchTerm) => {
      if (searchTerm && !searchHistory.value.includes(searchTerm)) {
        searchHistory.value.push(searchTerm)
      }
    }

    const removeSearch = (index) => {
      searchHistory.value.splice(index, 1)
    }

    const clearSearchHistory = () => {
      searchHistory.value = []
    }

    return {
      searchHistory,
      addSearch,
      removeSearch,
      clearSearchHistory
    }
  },
  {
    persist: {
      key: 'SEARCH_HISTORY'
    }
  }
)
